package com.huike.service.system.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.dto.SysPageQueryDTO;
import com.huike.dto.SysUserDTO;

import com.huike.mapper.system.SysUserMapper;
import com.huike.pojo.User;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.system.SysUserService;

import com.huike.vo.SysUserPageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@Service
public class SysUserServiceImpl  implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 用户管理分页查询
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult list(SysPageQueryDTO queryDTO) {
        //执行查询条件之前先校验一下数据
        if (null == queryDTO) {
            System.out.println("参数为空！");
        }
        if (null == queryDTO.getPageSize() || null == queryDTO.getPageNum() || queryDTO.getPageNum() <= 0 || queryDTO.getPageSize() <= 0) {
            queryDTO.setPageNum(1);
            queryDTO.setPageSize(10);
        }

        PageHelper.startPage(queryDTO.getPageNum(), queryDTO.getPageSize());
        Page<SysUserPageVO> page = sysUserMapper.selectDictList(queryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setRows(page.getResult());
        pageResult.setTotal(page.getTotal());
        pageResult.setCode(200);
        pageResult.setMsg("查询成功");
        return pageResult ;
    }

    /**
     * 修改用户状态
     *
     * @param
     * @return
     */
    @Override
    public void changeStatus(Integer status  ) {
        if (status == null) {
           log.error("修改用户状态失败");
        }
        sysUserMapper.changeStatus(status);
    }

    /**
     * 新增用户
     *
     * @param
     */
    @Override
    public void add(SysUserDTO  userDTO) {
            if (userDTO == null || userDTO.getUserName() == null || userDTO.getNickName() == null || userDTO.getPhonenumber() == null){
                log.error("参数为空");
            }
        String userName = sysUserMapper.selectUserName(userDTO);
            if (userName != null) {
                log.error("用户名已存在");
            }
        User user = new User();
            BeanUtils.copyProperties(userDTO,user);
            user.setCreateTime(LocalDateTime.now());
            if (userDTO.getPassword() == null){
                user.setPassword("123456");
            }
            sysUserMapper.add(user);

    }
//    * 修改用户
    @Override
    public void update(SysUserDTO userDTO) {

    }
//    * 根据id查询用户
    @Override
    public SysUserDTO selectById(Long id) {
        return null;
    }

    /**
     * 删除用户
     *
     * @param ids
     */
    @Override
    public void deleteUser(List<Long> ids) {
        sysUserMapper.deleteUser(ids);
    }


}
